package LeetCode.leetcode216;

import java.util.ArrayList;
import java.util.List;

public class Review1 {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        test(res, path, k, n, 1, 0);
        return res;
    }

    public void test(List<List<Integer>> res, List<Integer> path, int k, int n, int start, int sum) {
        if (path.size() == k) {
            if (sum == n) {
                res.add(new ArrayList<>(path));
            }
            return;
        }
        if (sum >= n) return;
        for (int i = start; i <= 9; i++) {
            path.add(i);
            test(res, path, k, n, start + 1, sum + i);
            path.remove(path.size() - 1);
        }
    }
}
